#include <iostream>
#include <string>
#include "../comm/httplib.h"
#include "oj_control.hpp"

using namespace httplib;
using namespace ns_control;


int main()
{
    Server svr;
    Control ctrl;

    svr.Get("/all_questions", [&ctrl](const Request &req, Response &resp){
        //  resp.set_content("这是所有题目的列表", "text/plain; charset=utf-8");
        std::string out_html;
        ctrl.AllQuestions(&out_html);
        resp.set_content(out_html, "text/html; charset=utf-8");
    });

    svr.Get(R"(/question/(\d+))", [&ctrl](const Request &req, Response &resp){
        std::string number = req.matches[1];
        std::string out_html;
        ctrl.Question(number, &out_html);
        resp.set_content(out_html, "text/html; charset=utf-8");
        // resp.set_content("这是指定一道题目: " + number, "text/html; charset=utf-8");
    });

    svr.Post(R"(/judge/(\d+))", [&ctrl](const Request &req, Response &resp){
        std::string number = req.matches[1]; 
        std::string out_html;
        ctrl.Judge(number, req.body, &out_html);
        resp.set_content(out_html, "application/json;charset=utf-8");
        // resp.set_content("对指定题目进行判题: " + number, "text/plain; charset=utf-8");
    });

    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0", 8080);
    return 0;
}
